package com.zhihuishu.micro.course.openapi.course.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.zhihuishu.micro.course.dto.CourseSpeakerDto;
import com.zhihuishu.micro.course.openapi.course.CourseSpeakerOpenService;
import com.zhihuishu.micro.course.openapi.course.dto.CourseSpeakerOpenDto;
import com.zhihuishu.micro.course.service.CourseSpeakerService;
import com.zhihuishu.remote.ErrorCode;
import com.zhihuishu.remote.RemoteException;
import com.zhihuishu.remote.RemoteResult;
import com.zhihuishu.toolkit.helper.BeanHelper;


@Service("courseSpeakerOpenService")
public class CourseSpeakerOpenServiceImpl implements CourseSpeakerOpenService{
	private Logger log = LoggerFactory.getLogger(CourseSpeakerOpenServiceImpl.class);
    @Resource
	private CourseSpeakerService courseSpeakerService;//教学团队service
    //教学团队或者是课程负责人新增时默认都是不可删除的
	@Override
	public RemoteResult<CourseSpeakerOpenDto> save(CourseSpeakerOpenDto csd) throws RemoteException {
		
		RemoteResult<CourseSpeakerOpenDto> result=new RemoteResult<CourseSpeakerOpenDto>();
		    if(csd == null){
		    	result.setSuccess(false);
		    	result.setResult(null);
		    	return null;
		    }
		try {
			CourseSpeakerDto courseSpeakerDtoParam = BeanHelper.transTo(csd, CourseSpeakerDto.class);
			courseSpeakerDtoParam = courseSpeakerService.insert(courseSpeakerDtoParam);
			CourseSpeakerOpenDto resultDto= BeanHelper.transTo(courseSpeakerDtoParam, CourseSpeakerOpenDto.class);
			result.setSuccess(true);
			result.setResult(resultDto);
		} catch (Exception e) {
			throw new RemoteException(e).setMessage("保存教师团队的信息出错!") ;
		}
		return result;
		
	}
    //更新操作
	@Override
	public RemoteResult<CourseSpeakerOpenDto> update(CourseSpeakerOpenDto csd) throws RemoteException {
		RemoteResult<CourseSpeakerOpenDto> result=new RemoteResult<CourseSpeakerOpenDto>();
		    if(csd==null){
				result.setSuccess(false);
				result.setErrorCode(ErrorCode.ARGUMENTS_ILLEGALITY);
				return result ;
		    }
		try {
			CourseSpeakerDto courseSpeaker = BeanHelper.transTo(csd, CourseSpeakerDto.class);
			courseSpeakerService.update(courseSpeaker);			
			CourseSpeakerDto cd = this.courseSpeakerService.findCourseSpeakerById(courseSpeaker.getId());
			CourseSpeakerOpenDto courseSpeakerOpenDto = BeanHelper.transTo(cd,CourseSpeakerOpenDto.class);
		    result.setResult(courseSpeakerOpenDto);
		    result.setSuccess(true);
			
		} catch (Exception e) {
			throw new RemoteException(e).setMessage("修改教师团队的信息出错!") ;
		}
		return result ;
	}
    //查询
	@Override
	public RemoteResult<List<CourseSpeakerOpenDto>> courseSpeakearList(long courseId) throws RemoteException {
		RemoteResult<List<CourseSpeakerOpenDto>> result= new RemoteResult<List<CourseSpeakerOpenDto>>();
		try {
			List<CourseSpeakerDto> courseSpeakerList = courseSpeakerService.searchCourseSpeakerList(courseId);
			List<CourseSpeakerOpenDto> courseSpeakerOpenList =  new ArrayList<CourseSpeakerOpenDto>();
			if (!CollectionUtils.isEmpty(courseSpeakerList)){
				for(CourseSpeakerDto course : courseSpeakerList){
					CourseSpeakerOpenDto courseSpeakerOpenDto = BeanHelper.transTo(course,CourseSpeakerOpenDto.class);
					courseSpeakerOpenList.add(courseSpeakerOpenDto);
				}
			}
			result.setResult(courseSpeakerOpenList);
			result.setSuccess(true);
		} catch (Exception e) {
			throw new RemoteException(e).setMessage("查询教师团队的信息出错!") ;
		}
		return result;
	}
    //删除只是更改数据库中的某个字段的状态
	@Override
	public RemoteResult<Void> delete(Integer id) throws RemoteException {
		    RemoteResult<Void>  result = new RemoteResult<Void>();
		    if(id == null){
				result.setSuccess(false);
				result.setErrorCode(ErrorCode.ARGUMENTS_ILLEGALITY);
				return result ;
		    }
		    try {
				courseSpeakerService.delete(id);
			} catch (Exception e) {
				log.error(e.getMessage());
				throw new RemoteException(e).setMessage("删除教师团队的信息出错!") ;
			}
			return result;
		   
	}
	//查询返回的条数
	@Override
	public RemoteResult<Integer> countCourseSpeaker(Integer id) throws RemoteException {
	    RemoteResult<Integer>  result = new RemoteResult<Integer>();
	    if(id == null){
			result.setSuccess(false);
			result.setErrorCode(ErrorCode.ARGUMENTS_ILLEGALITY);
			return result ;
	    }
	    try {
			Integer n =courseSpeakerService.countCourseSpeaker(id);
			        result.setResult(n);
			        result.setSuccess(true);
		} catch (Exception e) {
			throw new RemoteException(e).setMessage("查询教师团队数目的信息出错!") ;
		}
		return result;
	}
	//根据id查询相应的教师团队或者是课程负责人
	@Override
	public RemoteResult<CourseSpeakerOpenDto> findCourseSpeakerById(Integer id) throws RemoteException {
		RemoteResult<CourseSpeakerOpenDto>  result = new RemoteResult<CourseSpeakerOpenDto>();
	    if(id == null){
			result.setSuccess(false);
			result.setErrorCode(ErrorCode.ARGUMENTS_ILLEGALITY);
			return result ;
	    }
	       try {
			CourseSpeakerDto cd = this.courseSpeakerService.findCourseSpeakerById(id);
			   CourseSpeakerOpenDto courseSpeakerOpenDto = BeanHelper.transTo(cd,CourseSpeakerOpenDto.class);
			   result.setResult(courseSpeakerOpenDto);
			   result.setSuccess(true);
		} catch (Exception e) {			
			throw new RemoteException(e).setMessage("根据id查询教师团队的信息出错!") ;
		}
		return result;
	}
	@Override
	public RemoteResult<Map<String, Boolean>> findCourseSpeakerIsComplete(Long courseId) throws RemoteException {
		RemoteResult<Map<String, Boolean>> result = new RemoteResult<Map<String,Boolean>>();
		if(courseId == null){
			result.setSuccess(false);
			result.setExceptionStack(new Exception("课程ID不能为空"));
			result.setErrorCode(ErrorCode.ARGUMENTS_ILLEGALITY);
			return result ;
	    }
		
		try {
			Map<String, Boolean> isCompleteMap = courseSpeakerService.findCourseSpeakerIsComplete(courseId, 4);
			result.setResult(isCompleteMap);
			result.setSuccess(true);
		} catch (Exception e) {
			throw new RemoteException(e).setMessage("根据id查询教师团队是否完整出错!") ;
		}
		return result;
	}

	

}
